﻿Imports Entities
Imports Business
Imports DataAccess
Public Class IngresoPedido
    Private _PedidosPList As New List(Of Entities.PedidoP)

    Private Sub IngresoPedido_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        With cProveedores
            .DataSource = ProveedorBL.List
            .ValueMember = "PROVEEDOR"
            .DisplayMember = "NOMBRE"
        End With
    End Sub


#Region "Metodos Privados"
    Private Sub Titulo()
        'DEFININE LA FECHA ACTUAL 
        xFechaPedido.Text = Now.ToShortDateString

        gItemes.DataSource = _PedidosPList
        CalcularVacios()
        'ESTABLECE LA FUENTE Y EL ANCHO
        gItemes.Font = New Drawing.Font("Tahoma", Util.GridFontIngresos, FontStyle.Regular, GraphicsUnit.Point)
        gItemes.Width = 700
        gItemes.RowHeadersVisible = False
        gItemes.Columns("ItemId").Visible = False
        ' gItemes.Columns("STOCKGARANTIAS").Visible = False
        'gItemes.Columns("CANTIDADKILOS").Visible = False
        gItemes.Columns("GARANTIAId").Visible = False

        'COLUMNAS NO EDITABLES
        gItemes.Columns("Descripcion").ReadOnly = True
        gItemes.Columns("Kilos").ReadOnly = True
        gItemes.Columns("Stock").ReadOnly = True
        gItemes.Columns("StockGarantia").ReadOnly = True
        gItemes.Columns("CantidadPedido").ReadOnly = True

        'ESTABLECE LOS ENCABEZADOS
        gItemes.Columns("Descripcion").HeaderText = "Descripción"
        gItemes.Columns("Kilos").HeaderText = "Cantidad de Kilos"
        gItemes.Columns("Stock").HeaderText = "Stock Actual (Llenos)"
        gItemes.Columns("StockGarantia").HeaderText = "Garantías Disponibles (Vacíos)"
        gItemes.Columns("CantidadPedido").HeaderText = "Cantidad Pedido"

        'ESTABLECE EL ORDEN 
        gItemes.Columns("ItemId").DisplayIndex = 0
        gItemes.Columns("Descripcion").DisplayIndex = 1
        gItemes.Columns("Kilos").DisplayIndex = 2
        gItemes.Columns("STOCK").DisplayIndex = 3
        gItemes.Columns("StockGarantia").DisplayIndex = 4
        gItemes.Columns("CANTIDADPEDIDO").DisplayIndex = 5

        'ESTABLECE EL ANCHO DE COLUMNAS
        For Each col As DataGridViewColumn In gItemes.Columns
            col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        Next
        gItemes.Columns("CANTIDADPEDIDO").DefaultCellStyle.BackColor = Color.Azure
        'gItemes.Columns("ItemId").Width = 150
        'gItemes.Columns("Descripcion").Width = 250
        'gItemes.Columns("Kilos").Width = 150
        'gItemes.Columns("STOCK").Width = 150
        'gItemes.Columns("StockGarantia").Width = 150
        'gItemes.Columns("CANTIDADPEDIDO").Width = 100

        'ESTABLECE LA ALINEACION DE LOS ENCABEZADOS
        gItemes.Columns("ItemId").HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
        gItemes.Columns("Descripcion").HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
        gItemes.Columns("Kilos").HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
        gItemes.Columns("STOCK").HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
        gItemes.Columns("StockGarantia").HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
        gItemes.Columns("CANTIDADPEDIDO").HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter

        'ESTABLECE LA ALINEACION DEL CONTENIDO
        gItemes.Columns("ItemId").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        gItemes.Columns("Descripcion").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
        gItemes.Columns("Kilos").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        gItemes.Columns("STOCK").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        gItemes.Columns("StockGarantia").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        gItemes.Columns("CANTIDADPEDIDO").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter

    End Sub

    Private Sub CalcularVacios()
        For Each Fila As DataGridViewRow In gItemes.Rows
            Dim cantidad As Integer = 0
            Dim res As Integer = 0
            Fila.Cells("StockGarantia").Value = GarantiaBL.GetStock(Fila.Cells("GarantiaId").Value)
            For Each rec As DataGridViewRow In gItemes.Rows
                'recato la cantidad de todos los itemes de la misma garantia
                If rec.Cells("GarantiaId").Value = Fila.Cells("GarantiaId").Value Then
                    cantidad += rec.Cells("stock").Value + rec.Cells("cantidadpedido").Value
                End If
            Next
            res = Fila.Cells("StockGarantia").Value - cantidad
            For Each rec As DataGridViewRow In gItemes.Rows
                If rec.Cells("GarantiaId").Value = Fila.Cells("GarantiaId").Value Then
                    Fila.Cells("StockGarantia").Value = res
                End If
            Next
        Next

        CalcularTotales()
    End Sub
   
    Private Sub CalcularTotales()
        Dim Total As Integer = 0
        Dim TotalPrecio As Integer = 0
        For Each Fila As DataGridViewRow In gItemes.Rows
            Total += Fila.Cells("KILOS").Value * Fila.Cells("CANTIDADPEDIDO").Value
            'TotalPrecio += Fila.Cells("").Value * Fila.Cells("CANTIDADPEDIDO").Value
        Next
        lbKilos.Text = Total
    End Sub

    Private Function ValidarPedido() As Boolean
        Dim sum As Integer = 0
        For Each rec As DataGridViewRow In gItemes.Rows
            sum += rec.Cells("CantidadPedido").Value
        Next
        If sum > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

#End Region

    Private Sub cRutProveedor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cProveedores.SelectedIndexChanged
        If IsNumeric(cProveedores.SelectedValue) AndAlso cProveedores.SelectedIndex > -1 Then
            _PedidosPList = PedidoBL.ListP(cProveedores.SelectedValue)
            Titulo()
        End If
    End Sub

    Private Sub bGenerar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bGenerar.Click

        If Not ValidarPedido() Then
            Util.Mensaje("Debe Ingresar la Cantidad de Pedido de al Menos un Item")
            gItemes.Focus()
        Else
            Dim ped As New PEDIDOS
            With ped
                .FECHA = xFechaPedido.Value
                .PROVEEDOR = CInt(cProveedores.SelectedValue)
                .TOTALKILOS = CInt(lbKilos.Text)
            End With
            Dim lista As New List(Of DETALLEPEDIDO)
            For Each rec As DataGridViewRow In gItemes.Rows
                If rec.Cells("CantidadPedido").Value > 0 Then
                    Dim det As New DETALLEPEDIDO
                    With det
                        .ITEM = rec.Cells("itemId").Value
                        .CANTIDAD = CInt(rec.Cells("CantidadPedido").Value)
                    End With
                    lista.Add(det)
                End If
            Next
            If PedidoBL.Save(ped, lista) Then
                Util.Mensaje("El Pedido se ha Generado Exitosamente")
                Me.Dispose()
            Else
                Util.MensajeError("Error al Generar Pedido")
            End If
        End If
    End Sub

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        Dispose()
    End Sub

    Private Sub gItemes_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gItemes.CellEnter
        If e.ColumnIndex = gItemes.Columns.IndexOf(gItemes.Columns("CantidadPedido")) Then
            Using Form As New IngresoDato("Ingreso Cantidad de Pedido", "Ingrese Cantidad")
                Form.ShowDialog()
                If Form.DialogResult = DialogResult.OK Then
                    gItemes.Item(e.ColumnIndex, e.RowIndex).Value = Form.GetValue
                    CalcularVacios()
                End If
            End Using
        End If
    End Sub

    Private Sub cProveedores_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cProveedores.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub xFechaPedido_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles xFechaPedido.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub
End Class